package controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pojo.Taikhoan;
import pojo.Taikhoanadmin;
import dao.NhomsanphamDAO;
import dao.TaikhoanDAO;
import dao.TaikhoanadminDAO;

/**
 * Servlet implementation class MainPage
 */
@WebServlet("/Pages/MainPage.do")
public class MainPage extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public MainPage() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");

		HttpSession session = request.getSession();

		// Khởi tạo gái trị Tài Khoản để test
		TaikhoanDAO tkDAO = new TaikhoanDAO();
		Taikhoan tk = tkDAO.get(1686277);

		// Kiểm tra có phải là Admin hay không
		TaikhoanadminDAO tkadao = new TaikhoanadminDAO();
		Taikhoanadmin tka = tkadao.get(tk.getMataikhoan());
		if (null != tka)
			tk.setLaadmin(true);
		session.setAttribute("taiKhoan", tk);

		// Khởi tạo giá trị Danh Sách Danh Mục (Đưa vào Menu)
		NhomsanphamDAO nspDAO = new NhomsanphamDAO();
		session.setAttribute("nhomSanPham", nspDAO.getList());

		String url = "../index.jsp";
		request.setAttribute("page", "TrangGioHang");
		// request.setAttribute("page", "TrangQuanLyDonDatHang");

		RequestDispatcher rd = request.getRequestDispatcher(url);
		rd.forward(request, response);
	}

}
